#include "Metgine/Graphics/FXManager.hpp"

#include "Metgine/Core/Engine.hpp"


namespace Metgine
{
namespace Graphics
{
FXManager::FXManager()
{

}

FXManager::~FXManager()
{

}

bool FXManager::IsRegistered(Effect* effect)
{

    std::list<Effect*>::iterator i = m_effects.begin();

    while (i != m_effects.end())
    {

        if ((*i) == effect)
            return true;

        i ++;

    }

    return false;

}

Effect* FXManager::RegisterEffect(Effect* effect)
{

    if (!IsRegistered(effect))
        m_effects.push_back(effect);

    return effect;

}
bool FXManager::UnregisterEffect(Effect* effect)
{

    std::list<Effect*>::iterator i = m_effects.begin();

    while (i != m_effects.end())
    {

        if ((*i) == effect)
        {

            delete (*i);

            m_effects.erase(i);
            return true;

        }

        i ++;

    }

    return false;

}

void FXManager::ClearEffects()
{
    std::list<Effect*>::iterator i = m_effects.begin();

    while (i != m_effects.end())
    {
        delete (*i);
        i ++;
    }
    m_effects.clear();
}

void FXManager::Update(float elapsedTime)
{
    std::list<Effect*>::iterator i = m_effects.begin();

    while (i != m_effects.end())
    {
        Effect* effect = (Effect*)(*i);

        if (effect->IsAlive())
        {
            effect->Update(elapsedTime);
            i ++;
        }
        else
        {
            delete effect;
            effect = NULL;
            i = m_effects.erase(i);
        }
    }
}

void FXManager::Render()
{
    std::list<Effect*>::iterator i = m_effects.begin();

    while (i != m_effects.end())
    {

        (*i)->Render(Engine::Get()->GetRenderer()->GetWindow());

        i ++;
    }
}

}; // Graphics
}; // Metgine
